import classNames from 'classnames'
import { useEffect, useRef } from 'react'
import styles from './index.module.scss'

interface Props
  extends React.DetailedHTMLProps<React.TextareaHTMLAttributes<HTMLTextAreaElement>, HTMLTextAreaElement> {
  className?: string
  value?: string
}

export default function Textarea({ className, value, maxLength = 100, ...res }: Props) {
  const textareaRef = useRef<HTMLTextAreaElement>(null)
  useEffect(() => {
    textareaRef.current?.setSelectionRange(-1, -1)
  }, [])
  return (
    <div className={styles.root}>
      {/* 文本输入框 */}
      <textarea
        ref={textareaRef}
        {...res}
        maxLength={maxLength}
        value={value}
        className={classNames('textarea', className)}
      />
      {/* 当前字数/最大允许字数 */}
      <div className="count">
        {value ? value.length : 0}/{maxLength}
      </div>
    </div>
  )
}
